﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Permissions;
using System.Collections;
using System.Linq;

using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using uacars;

namespace CADLibraryClass
{
    public class ProductosCAD
    {
        private string cadenaConexion;

        public ProductosCAD()
        {
            cadenaConexion = Singleton.Instancia.Cadena;
        }


        //IMPLEMENTACION DE LAS FUNCIONES DE ACCESO A DATOS
        public bool AltaVehiculo(int id, string matricula, string marca, string modelo, string categoria, string color, float precioAlquiler, float precioVenta,string path)
        {
            bool estadoOperacion = false;

            try
            {
                DataSet bdvirtual = new DataSet();

                SqlDataAdapter da = new SqlDataAdapter("select * from vehiculos", cadenaConexion);
                da.Fill(bdvirtual, "vehiculos");

                DataTable dt = new DataTable();
                dt = bdvirtual.Tables["vehiculos"];

                DataRow nuevaFila = dt.NewRow();
                nuevaFila[0] = id;
                nuevaFila[1] = marca;
                nuevaFila[2] = modelo;
                nuevaFila[3] = matricula;
                nuevaFila[4] = color;
                nuevaFila[5] = precioAlquiler;
                nuevaFila[6] = precioVenta;
                nuevaFila[7] = categoria;
                nuevaFila[8] = path;


                dt.Rows.Add(nuevaFila);

                SqlCommandBuilder cBuilder = new SqlCommandBuilder(da);

                da.Update(bdvirtual, "vehiculos");

                dt.AcceptChanges();
            }


            catch (SqlException sqlex)
            {
                throw new Exception("Error insertando vehiculo en uacarsBD: " + matricula, sqlex);
            }

            catch (Exception ex)
            {
                throw ex;
            }
            estadoOperacion = true;

            return (estadoOperacion);
        }

        public bool ModificarVehiculo(int id, string matricula, string marca, string modelo, string categoria, string color, float precioAlquiler, float precioVenta,string path)
        {
            bool estadoOperacion;
            try
            {
                DataSet bdvirtual = new DataSet();

                SqlDataAdapter da = new SqlDataAdapter("select * from vehiculos", cadenaConexion);
                da.Fill(bdvirtual, "vehiculos");

                DataTable dt = new DataTable();
                dt = bdvirtual.Tables["vehiculos"];


                DataRow fila = dt.Select("matricula LIKE '" + matricula + "'")[0];
                fila["id"] = id;
                fila["matricula"] = matricula;
                fila["marca"] = marca;
                fila["modelo"] = modelo;
                fila["categoria"] = categoria;
                fila["color"] = color;
                fila["precioalquiler"] = precioAlquiler;
                fila["precioventa"] = precioVenta;
                fila["urlimagen"] = path;




                bdvirtual.Tables["vehiculos"].LoadDataRow(fila.ItemArray, true);
                SqlCommandBuilder cBuilder = new SqlCommandBuilder(da);

                da.Update(bdvirtual, "vehiculos");

                dt.AcceptChanges();
            }


            catch (SqlException sqlex)
            {
                throw new Exception("Error modificando el vehiculo en uacarsBD: " + matricula, sqlex);
            }

            catch (Exception ex)
            {
                throw ex;
            }
            estadoOperacion = true;

            return (estadoOperacion);
        }


        public bool BorrarVehiculo(int id)
        {
            try
            {
                DataSet bdvirtual = new DataSet();

                SqlDataAdapter da = new SqlDataAdapter("select * from vehiculos", cadenaConexion);
                da.Fill(bdvirtual, "vehiculos");

                DataTable dt = new DataTable();
                dt = bdvirtual.Tables["vehiculos"];

                DataRow[] filaBorrar;
                filaBorrar = dt.Select("id = " + id);
                filaBorrar[0].Delete();

                SqlCommandBuilder cBuilder = new SqlCommandBuilder(da);

                da.Update(bdvirtual, "vehiculos");

                dt.AcceptChanges();

            }

            catch (SqlException sqlex)
            {
                throw new Exception("Error borrando el vehiculo en uacarsBD: " + id, sqlex);
            }

            catch (Exception ex)
            {
                throw ex;
            }
            return true;
        }



        public DataTable BuscarVehiculo()
        {
            DataSet bdvirtual = new DataSet();

            SqlDataAdapter da = new SqlDataAdapter("select * from vehiculos", cadenaConexion);

            da.Fill(bdvirtual, "vehiculos");

            DataTable dt = new DataTable();
            dt = bdvirtual.Tables["vehiculos"];

            return (dt);

        }

        public DataTable BuscarVehiculo(int id, string matricula, string marca, string modelo, string categoria, string color, float precioAlquiler, float precioVenta)
        {
            string filtro = "";

            if (id != 0) { if (filtro != "") filtro += " AND "; filtro += "ID = " + id; }
            if (matricula != "") { if (filtro != "") filtro += " AND "; filtro += "matricula LIKE '" + matricula + "'"; }
            if (marca != "") { if (filtro != "") filtro += " AND "; filtro += "marca LIKE '" + marca + "'"; }
            if (modelo != "") { if (filtro != "") filtro += " AND "; filtro += "modelo LIKE '" + modelo + "'"; }
            if (categoria != "") { if (filtro != "") filtro += " AND "; filtro += "categoria LIKE '" + categoria+"'"; }
            if (color != "") { if (filtro != "") filtro += " AND "; filtro += "color LIKE '" + color + "'"; }
            if (precioAlquiler != 0.0F) { if (filtro != "") filtro += " AND "; filtro += "precioalquiler = " + precioAlquiler; }
            if (precioVenta != 0.0F) { if (filtro != "") filtro += " AND "; filtro += "precioventa = " + precioVenta; }
        
            string nombreTabla = "vehiculos";
            DataSet ds = new DataSet();
            ds.Tables.Add(nombreTabla);
            SqlConnection conn = new SqlConnection(cadenaConexion);
            SqlDataAdapter da = new SqlDataAdapter();
            SqlCommand co = new SqlCommand("", conn);
            if (filtro == "")
                co.CommandText = "select * from " + nombreTabla;
            else
                co.CommandText = "select * from " + nombreTabla + " WHERE " + filtro;
            da.SelectCommand = co;
            da.Fill(ds, nombreTabla);

            return ds.Tables[nombreTabla];
        }
    }
}